<template>
  <div class="question-form-container">
    <el-card class="question-form" style="margin-top: -150px">
      <div slot="header" class="clearfix">
        <span><i class="el-icon-edit-outline"></i> 提交新问题</span>
      </div>
      <el-form :model="questionForm" :rules="rules" ref="questionForm" label-position="top">
        <el-form-item label="标题" prop="title">
          <el-input
              v-model="questionForm.title"
              placeholder="请输入问题标题"
              prefix-icon="el-icon-question">
          </el-input>
        </el-form-item>

        <el-form-item label="问题描述" prop="description">
          <el-input
              type="textarea"
              v-model="questionForm.description"
              :rows="6"
              placeholder="请详细描述您的问题"
          ></el-input>
        </el-form-item>

        <el-form-item label="标签" prop="tag">
          <el-input
              v-model="questionForm.tag"
              :rows="6"
              placeholder="请详细描述您的问题"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('questionForm')" :loading="submitting">
            <i class="el-icon-check"></i> 提交问题
          </el-button>
          <el-button @click="resetForm('questionForm')" icon="el-icon-refresh-left">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      questionForm: {
        title: '',
        description: '',
        tag: '',
      },
      rules: {
        title: [
          { required: true, message: '请输入问题标题', trigger: 'blur' },
          { min: 5, max: 100, message: '长度在 5 到 100 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入问题描述', trigger: 'blur' },
          { min: 10, max: 1000, message: '长度在 20 到 1000 个字符', trigger: 'blur' }
        ]
      },
      submitting: false
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.submitting = true;
          console.log('submit!', this.questionForm);
          // Here you would typically send the form data to your backend
          axios.post('/api/question/create', this.questionForm, {
            headers: { token: this.token}
          })
              .then(() => {
                this.$message({
                  message: '问题提交成功！',
                  type: 'success',
                  duration: 2000
                });
                this.resetForm(formName);
              })
              .catch(error => {
                console.error('Error submitting question:', error);
                this.$message.error('问题提交失败，请稍后重试');
              })
              .finally(() => {
                this.submitting = false;
              });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.questionForm.fileList = [];
    },
  },
  created() {
    this.token = localStorage.getItem("token");
    if (!this.token) {
      this.$router.push("/login");
    } else {
      this.getData();
      this.load();
    }
  },
};
</script>

<style scoped>
.question-form-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;
  padding: 20px;
}

.question-form {
  width: 100%;
  max-width: 800px;
  background-color: #ffffff;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 0 1px 3px rgba(0, 0, 0, 0.08);
}

.el-card__header {
  background-color: #409EFF;
  color: #ffffff;
  font-size: 18px;
  font-weight: bold;
  padding: 15px 20px;
}

.el-form-item {
  margin-bottom: 22px;
}

.el-select {
  width: 100%;
}

.el-upload {
  width: 100%;
}

.el-form-item__label {
  font-weight: bold;
  color: #606266;
}

.el-input__inner,
.el-textarea__inner {
  border-radius: 4px;
}

.el-input__inner:focus,
.el-textarea__inner:focus {
  border-color: #409EFF;
}

.el-select .el-input__inner:focus {
  border-color: #409EFF;
}

.el-button--primary {
  background-color: #409EFF;
  border-color: #409EFF;
}

.el-button--primary:hover,
.el-button--primary:focus {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.el-upload__tip {
  line-height: 1.2;
  padding-top: 5px;
  color: #909399;
}

.el-card {
  border-radius: 8px;
  overflow: hidden;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

/* Add some animation */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-20px); }
  to { opacity: 1; transform: translateY(0); }
}

.question-form {
  animation: fadeIn 0.5s ease-out;
}

.el-form-item {
  transition: all 0.3s ease;
}

.el-form-item:hover {
  transform: translateY(-2px);
}

/* Custom scrollbar for textarea */
.el-textarea__inner::-webkit-scrollbar {
  width: 8px;
}

.el-textarea__inner::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

.el-textarea__inner::-webkit-scrollbar-thumb {
  background: #888;
  border-radius: 4px;
}

.el-textarea__inner::-webkit-scrollbar-thumb:hover {
  background: #555;
}
</style>